/*
 * Copyright 2009 Jinbo Lin. All rights reserved.
 * 
 * cn.edu.pku.dao.impl HumanResourcesManagementSystem
 *
 * Created on 2009-12-20
 */
package cn.edu.pku.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.pku.dao.DepartmentmgtDao;
import cn.edu.pku.model.Departmentmgt;

/**
 * @author Jinbo Lin
 * 
 *  
 */
public class DepartmentmgtDaoHibernate extends HibernateDaoSupport implements
		DepartmentmgtDao {

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.DepartmentmgtDao#delete(int)
	 */
	@Override
	public void delete(int id) {
		if (null != this.get(id)) {
			this.getHibernateTemplate().delete(
					this.getHibernateTemplate().get(Departmentmgt.class, id));
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.DepartmentmgtDao#delete(cn.edu.pku.model.Departmentmgt)
	 */
	@Override
	public void delete(Departmentmgt departmentmgt) {
		if (null != this.get(departmentmgt.getDepartmentId())) {
			this.getHibernateTemplate().delete(departmentmgt);
		}
	}
	
	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.DepartmentmgtDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Departmentmgt> findAll() {
		String hql = "from Departmentmgt";
		return this.getHibernateTemplate().find(hql);
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.DepartmentmgtDao#get(int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Departmentmgt get(int id) {
		String hql = "from Departmentmgt departmentmgt where departmentmgt.departmentId=?";
		List<Departmentmgt> departmentmgts = this.getHibernateTemplate().find(
				hql, new Integer(id));
		Departmentmgt departmentmgt = (null != departmentmgts && 0 != departmentmgts.size()) ? departmentmgts.get(0) : null;
		return departmentmgt;
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.DepartmentmgtDao#save(cn.edu.pku.model.Departmentmgt)
	 */
	@Override
	public Serializable save(Departmentmgt departmentmgt) {
		if(null == this.get(departmentmgt.getDepartmentId())) {
			this.getHibernateTemplate().save(departmentmgt);
		} else {
			departmentmgt = null;
		}
		return departmentmgt;
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.DepartmentmgtDao#update(cn.edu.pku.model.Departmentmgt)
	 */
	@Override
	public void update(Departmentmgt departmentmgt) {
		if (null != this.get(departmentmgt.getDepartmentId())) {
//			this.getHibernateTemplate().saveOrUpdate(department);
			this.getHibernateTemplate().update(departmentmgt);
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.pku.dao.DepartmentmgtDao#tDelete(int)
	 */
	@Override
	public void tDelete(int id) {
		Departmentmgt deptmgt = this.get(id);
		deptmgt.setRemark("Removed");
		this.save(deptmgt);
	}

//	public static void main(String[] args) {
//		Resource resource = new ClassPathResource("applicationContext.xml");
//		BeanFactory factory = new XmlBeanFactory(resource);
//		HibernateTemplate hibernateTemplate = (HibernateTemplate) factory
//				.getBean("hibernateTemplate");
//		DepartmentmgtDaoHibernate ddh = new DepartmentmgtDaoHibernate();
//		ddh.setHibernateTemplate(hibernateTemplate);
//
//		Departmentmgt deptmgt = null;
//
//		/*deptmgt = new Departmentmgt(2, 1, 1, "yes", new Date());
//		ddh.save(deptmgt);*/
//		
//		/*deptmgt = ddh.get(1);
//		System.out.println(deptmgt.getCreatorId());
//		System.out.println(deptmgt.getAuditorId());
//		System.out.println(deptmgt.getAudit());
//		System.out.println(deptmgt.getDateTime());*/
//
//		/*deptmgt = ddh.get(1);
//		deptmgt.setDepartmentId(10);
//		deptmgt.setAuditorId(3);
//		ddh.update(deptmgt);*/
//
//		/*List<Departmentmgt> deptmgts = ddh.findAll();
//		for (Departmentmgt t : deptmgts) {
//			System.out.println(t.getAuditorId());
//		}*/
//		
//		/*ddh.delete(2);*/
//		
//		/*deptmgt = new Departmentmgt(2, 1, 2, "yes", new Date());
//		ddh.delete(deptmgt);*/
//	}
}
